package com.example.delish.ui.recipes

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.VisibleForTesting
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import com.example.delish.model.SearchType
import com.example.delish.ui.recipes.graph.HomeNavGraph
import com.example.delish.ui.theme.DelishComposeTheme
import com.google.accompanist.insets.ProvideWindowInsets
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class HomeFragment : Fragment() {

    @ExperimentalFoundationApi
    @ExperimentalAnimationApi
    @VisibleForTesting
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply {
            setContent {
                DelishComposeTheme {
                    ProvideWindowInsets {
                        HomeNavGraph(
                            onCuisineSearch = { cuisine ->
                                val action =
                                    HomeFragmentDirections.goToSearchScreen(
                                        cuisine,
                                        SearchType.CUISINE
                                    )
                                findNavController().navigate(action)
                            },
                            onIngredientSearch = { query ->
                                val action =
                                    HomeFragmentDirections.goToSearchScreen(query, SearchType.QUERY)
                                findNavController().navigate(action)
                            },
                            onDetails = { recipeId ->
                                val action =
                                    HomeFragmentDirections.goToRecipesDetails(recipeId)
                                findNavController().navigate(action)
                            }
                        )
                    }
                }
            }
        }
    }
}